<template>
    <el-dialog :close-on-click-modal="false" :visible="keyFlag" class="zdy-dialog" width="700px" :title="$t('reset password')"
               :lock-scroll="false" :before-close="handleCloseDialog">
        <el-form :model="form" class="pop-form-box" :rules="rules" ref="ruleForm">
            <!--用户名-->
            <el-form-item :label="$t('username')" v-show="false">
                <el-input v-model="form.userName" type="text" disabled=""></el-input>
            </el-form-item>
            <!--旧密码-->
            <!--<el-form-item :label="$t('Old password')" prop="oldPassword">-->
                <!--<el-input autocomplete="new-password" v-model="form.oldPassword" type="password" show-password></el-input>-->
            <!--</el-form-item>-->
            <!--新密码-->
            <el-form-item :label="$t('new password')" prop="newPassword">
                <el-input v-model="form.newPassword" clearable type="password" show-password></el-input>
            </el-form-item>
            <!--确认新密码-->
            <el-form-item :label="$t('Confirm new password')" prop="passwordAck">
                <el-input v-model="form.passwordAck" clearable type="password" show-password></el-input>
            </el-form-item>
            <div class="footBtnBox">
                <el-button size="small" type="handle" @click="handleCloseDialog">{{this.i18n.t('Cancel')}}</el-button>
                <el-button size="small" type="primary" @click="submitForm">{{this.i18n.t('save')}}</el-button>
            </div>
        </el-form>
    </el-dialog>
</template>

<script>
    import { getLocalUserInfo } from "@/assets/common/js/global";
import {accountinfoUpdate} from "@/api/zhgl";
    export default {
        name: "keys",
        props: {
            keyFlag: {
                type: Boolean,
                default: false
            },
            editTableRow: {
                type: Object,
                defautl: () => {
                    return {}
                }
            }
        },
        data(){
            var validatePasswordAck = (rule, value, callback) => {
                // debugger
                console.log(value);
                if (value == ''||value==undefined) {
                    // 请再次输入密码
                    callback(new Error(this.i18n.t('Please enter your password again')));
                }else if(!this.$rules.password.reg.test(value)){
                    callback(new Error(this.i18n.t('Only English and numbers can be entered')+' '+this.$rules.password.errMsg+" " + this.$rules.password.min + '~' + this.$rules.password.max +' ' + this.i18n.t('bits')));
                }else if (value!=this.form.newPassword) {
                    // 两次输入密码不一致
                    callback(new Error(this.i18n.t('The two password does not match！')));
                }else {
                    callback();
                }
            };
            var validateNewPassword = (rule, value, callback) => {
                // debugger
                console.log(value);
                if (value == ''||value==undefined) {
                    // 请输入新密码
                    callback(new Error(this.i18n.t('Please enter a new password')));
                }else if(value == this.form.userName){
                    // 新密码不能和用户名相同
                    callback(new Error(this.i18n.t('The new password cannot be the same as the username')));
                }else if (!this.$rules.password.reg.test(value)) {
                    callback(new Error(this.i18n.t('Only English and numbers can be entered')+' '+this.$rules.password.errMsg+" " + this.$rules.password.min + '~' + this.$rules.password.max + ' ' + this.i18n.t('bits')));
                }else {
                    callback();
                }
            };
            var validateOldPassword = (rule, value, callback) => {
                // debugger
                console.log(value);
                if (value == ''||value==undefined) {
                    // 请输入旧密码
                    callback(new Error(this.i18n.t('Please enter your old password')));
                }else if (!this.$rules.password.reg.test(value)) {
                    callback(new Error(this.i18n.t('Only English and numbers can be entered')+' '+this.$rules.password.errMsg+" " + this.$rules.password.min + '~' + this.$rules.password.max + ' ' + this.i18n.t('bits')));
                }else {
                    callback();
                }
            };
            return{
                rules: {
                    oldPassword:[
                        { validator: validateOldPassword,required: true, trigger: 'blur' }
                    ],
                    passwordAck:[
                        { validator: validatePasswordAck,required: true, trigger: 'blur' }
                    ],
                    newPassword:[
                        { validator: validateNewPassword,required: true, trigger: 'blur' }
                    ]
                },
                value: 0,
                saveDisabled:false,
                form:{
                    userName:'',
                    newPassword:'',
                    oldPassword:'',
                    passwordAck:''
                }
            }
        },
        created() {
            this.form = this.editTableRow
            this.form.password = ''
        },
        methods:{
            /**
             * @description  关闭弹窗事件
             * @return not
             * @author xiongyi 2022-06-29 16:48:08
             **/
            handleCloseDialog() {
                this.$emit('update:keyFlag', false);
                this.$emit('update:editTableRow', null);
                if (this.fileName) {
                    this.fileName = "";
                    this.$refs.upload.clearFiles()
                }
            },
            // 修改回调
            accountinfoUpdate(params) {
                accountinfoUpdate(params).then((res) => {
                    if (res.code == 200) {
                        // 密码重置成功
                        this.W.$tips("success", this.i18n.t("Password reset successful"));
                        this.parentUpdate();
                    }
                }).catch().finally(() => {
                })
            },
            // 刷新列表
            parentUpdate() {
                this.$emit("update:editFlag",false);
                this.$parent.$refs.globalTable.loading_data();
            },
            submitForm() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        this.$emit('update:keyFlag', false);

                        const sendData = JSON.parse(JSON.stringify(this.form));
                        console.log(142,sendData);
                        let params = {
                            userName:sendData.userName,
                            name:sendData.name,
                            role:sendData.role,
                            id:sendData.id,
                            password:sendData.newPassword,
                            createBy:sendData.createBy
                        }
                        this.accountinfoUpdate(params)
                    }
                })

            },
        },
        mounted(){
            // this.createBy=getLocalUserInfo().username
        }
    }
</script>

<style scoped>

</style>